import GamePage from "./GamePage";
import PlatUtils from "./PlatUtils";
import { utils } from "./Utils";
import MoreGamesPanel from "./MoreGamesPanel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RecommendGamesNode extends cc.Component {

    @property(cc.Prefab)
    prefab : cc.Prefab = null;

    _moreGamesPanel : MoreGamesPanel = null;

    _pageView: cc.PageView = null;
    _content: cc.Node = null;
    _gamePageNode: cc.Node = null;

    _isContentFilled: boolean = false;

    _scrollInterval:number = 3;
    _timeTmp:number = 0;

    onLoad() {
        let pageViewNode: cc.Node = this.node.getChildByName("PageView");
        this._pageView = pageViewNode.getComponent(cc.PageView);
        this._content = this._pageView.content;

        this._gamePageNode = this._content.getChildByName("GamePage");

        let panel = cc.instantiate(this.prefab);
        this._moreGamesPanel = panel.getComponent("MoreGamesPanel");
        cc.director.getScene().addChild(panel);
    }

    update(dt: number) {
        if (!this._isContentFilled) {
            this._updateContent();
        }

        if(this._isContentFilled){
            this._timeTmp+=dt;
            if(this._timeTmp>=this._scrollInterval){
                this._timeTmp=0;
                let curIndex = this._pageView.getCurrentPageIndex();
                curIndex++;
                if(curIndex >= this._pageView.getPages().length){
                    curIndex = 0;
                }

                cc.log("curIndex = ",curIndex);
                this._pageView.scrollToPage(curIndex, 0.5);
            }
        }
    }

    _updateContent() {
        if (PlatUtils.IsWechat) {
            if (utils.wechatTool && utils.wechatTool.configData && utils.wechatTool.configData.game_list) {
                this._isContentFilled = true;

                let gameList = utils.wechatTool.configData.game_list;

                let gamePageList = [];
                let index : number = 0;
                for (let i = 0; i < gameList.length; i++) {
                    gamePageList.push(gameList[i]);
                    if(index == 3){
                        index = 0;

                        let gamePageNode = cc.instantiate(this._gamePageNode);
                        let gamePage : GamePage = gamePageNode.getComponent("GamePage");
                        cc.log("GamePageList : ", gamePageList.length);
                        gamePage.init(gamePageList);
                        this._pageView.addPage(gamePageNode);

                        gamePageList = [];
                    }else{
                        index++;
                    }
                }

                this.scheduleOnce((dt:number)=>{
                    this._content.width = this._content.children[0].width*this._content.childrenCount;
                },0.02);


                this._gamePageNode.destroy();
            }
        }
    }

    onMoreGamesBtnHandler(event:any, data:any){
        cc.log("Show More Games Panel");
        this._moreGamesPanel.show();
    }
}
